//
//  WBTitleBtn.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/14.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

class WBTitleBtn: UIButton {
    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: .normal)
        }else{
            setTitle(title! + " ", for: .normal)
            
            setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: .normal)
            setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: .selected)
        }
        
        setTitleColor(UIColor.darkGray, for: .normal)
        setTitleColor(UIColor.black, for: .highlighted)
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
            let imageView = imageView else {
                return
        }
        
        titleLabel.frame = CGRect(x: 0, y: titleLabel.frame.origin.y, width: titleLabel.bounds.width, height: titleLabel.bounds.height)
        imageView.frame = CGRect(x: titleLabel.bounds.width, y: imageView.frame.origin.y, width: imageView.bounds.width, height: imageView.bounds.height)
    }
}
